package com.jsonjavaoverlay.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;

public class CustomerApiCallback implements RequestCallback {
	public List<JSOModel> customer;
	public void onResponseReceived(Request request, Response response) {
        if (response.getStatusCode() == 200) {
            JsArray<JSOModel> data = JSOModel.arrayFromJson(response.getText());
            customer = new ArrayList<JSOModel>();
            for (int i = 0; i < data.length(); i++) {
                customer.add(new Customer(data.get(i)).getModel());
            }
        } else {
            onError(request, new RequestException(response.getText()));
        }
    }

    public void onError(Request request, Throwable throwable) {
        Window.alert("Error" + throwable.getMessage());
    }

}
